import axios from 'axios'
import newConfig from '@/config/newConfig.js'
import vuexStore from '@/store/index.js'
import store from '@/store/index.js'
import {
  encrypt
} from '@/utils/crypto'

var qs = require('qs');

const jsonHeader = {
  'Content-Type': 'application/json'
};
const header = {
  'Content-Type': 'application/x-www-form-urlencoded',
};

let token = getCookie('token');         
var tokenInstance = axios.create({
  headers: {
    'token': !token ? "" : token
  }
});
function getCookie(cookie_name) {
  var allcookies = document.cookie;
  var cookie_pos = allcookies.indexOf(cookie_name);
  if (cookie_pos != -1) {
      cookie_pos = cookie_pos + cookie_name.length + 1; 
      var cookie_end = allcookies.indexOf(";", cookie_pos);
      if (cookie_end == -1) {
          cookie_end = allcookies.length;
      }
      var value = unescape(allcookies.substring(cookie_pos, cookie_end)); 
  }
  return value;
}

let axiosInstance = axios.create();

axiosInstance.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  console.log('错误---', error);
  return null;
});

const baseUrl = {
  cloud: '/cloud/',
  account: '/account/',
  dashboard: '/dashboard/',
}

// if ( process.env.NODE_ENV === 'development' ) {
//     baseUrl.account = '/proxy' + baseUrl.account;
// }

export const dashboard = {

  getQuota() {
    const url = `${ baseUrl.cloud }resource/quota`;

    const promise = axiosInstance.get(url);

    return promise;

  },
  getStatistic() {
    const url = `${ baseUrl.cloud }resource/statistic`;

    const promise = axiosInstance.get(url);

    return promise;
  }
}

export const user = {

  getProfile() {
    const url = `${ baseUrl.account }profile`;

    const promise = axiosInstance.get(url)

    return promise;
  },

  updateNickname(nickname) {

    const url = `/op${ baseUrl.account }profile/name`;

    const params = {
      name: nickname
    };

    const promise = axiosInstance.post(url, qs.stringify(params), {
      headers: header
    })

    return promise;

  },

  updateEmail(email, password) {

    const url = `/op${ baseUrl.account }profile/email`;

    const params = {
      email,
      'hash_password': encrypt(password)
    };

    const promise = axiosInstance.post(url, qs.stringify(params), {
      headers: header
    })

    return promise;

  },

  updatePassword(oldPassword, password) {

    const url = `/op${ baseUrl.account }profile/pwd`;

    const params = {
      'hash_password': encrypt(oldPassword),
      'hash_password1': encrypt(password)
    };

    const promise = axiosInstance.post(url, qs.stringify(params), {
      headers: header
    })

    return promise;

  },

  getAKList() {
    const url = `${ baseUrl.cloud }ak`;

    const promise = axiosInstance.get(url)

    return promise;
  },

  addAK(type, trust) {

    const url = `${ baseUrl.cloud }ak`;

    const promise = axiosInstance.post(url, {
      type,
      trust
    })

    return promise;

  },

  removeAK(key) {

    const url = `${ baseUrl.cloud }ak/${ key }`;

    const promise = axiosInstance.delete(url)

    return promise;

  },

  updateAKTrust(key, trust) {
    const url = `${ baseUrl.cloud }ak/${ key }`;

    const promise = axiosInstance.put(url, {
      trust
    })

    return promise;
  }
}

export const plan = {

  checkVoucher(voucherId) {

    const url = `/op${ baseUrl.account }plan/voucher/check/`;

    const params = {
      voucherId
    };

    const promise = axiosInstance.post(url, qs.stringify(params), {
      headers: header
    })

    return promise;

  },

  calc() {

    const url = `/op${ baseUrl.account }plan/calc`;

    const promise = axiosInstance.post(url)

    return promise;

  }
}

export const publicData = {

  getStatus(params, success, error) {
    const url = `${baseUrl.mosaic}getMosaicStatus`

    axiosInstance.get(url, {
        params
      })
      .then(success)
      .catch(error)
  },

  list(params, success, error) {
    // const url = `/s/public_data/list/`;
    let url = `/s/public_data/list`;
    let userInfo = vuexStore.state.userInfo;
    // console.log(userInfo);
    if(userInfo&&userInfo.role=='admin'){
      url = `/s/public_data/list_all`;
    }
    axiosInstance.get(url, {
        params
      })
      .then(success)
      .catch(error)
  },

  refresh(uid, success, error) {
    const url = `/s/public_data/${uid}/thumb`;

    axiosInstance.get(url)
      .then(success)
      .catch(error)
  },

  info(uid) {
    return axiosInstance.get(`/s/public_data/${uid}/info`);
  },

  remove(uid) {
    const url = `${ baseUrl.cloud }public_data/${uid}`

    return axiosInstance.delete(url);
  },
};

export const publicImagery = {

  info(uid) {
    return axiosInstance.get(`/s/public_imagery/${uid}/info`);
  },
  list(params, success, error) {
    const url = `/s/public_imagery/list`;

    axiosInstance.get(url, {
        params
      })
      .then(success)
      .catch(error)
  },
  remove(uid) {
    const url = `${ baseUrl.cloud }public_imagery/${uid}`

    return axiosInstance.delete(url);
  },
};

export const data = {

  list(params, success, error) {
    const url = `/s/data/list_1`;

    axiosInstance.get(url, {
        params
      })
      .then(success)
      .catch(error)
  },

  remove(uid) {
    let url = `${ baseUrl.cloud }data_2/${uid}`;
    if(this.isPrivateApplyData()){
      url = `${ baseUrl.cloud }public_data/${uid}`;
    }
    return axiosInstance.delete(url);
  },
  isPrivateApplyData(){
    console.log(store.state.data_nav_key);
    let data_nav_key = store.state.data_nav_key;
    let flag = document.querySelector('.private-upload-res').classList.contains('active');
    if(data_nav_key == 'private-data' && !flag){
      return true;
    }
    return false;
  },
  share(uid, params, success, error) {
    const url = `/s/data/share/${uid}`;

    axiosInstance({
        method: 'post',
        url: url,
        data: qs.stringify(params),
        headers: header
      })
      .then(success)
      .catch(error)
  },

  shareStatus(uid, jobId, success, error) {
    const url = `/s/data/share/${uid}/checkJob/${jobId}`;

    axiosInstance.get(url)
      .then(success)
      .catch(error)
  },

  refresh(uid, success, error) {
    const url = `/s/data/${uid}/thumb/refresh`;

    axiosInstance({
        method: 'post',
        url: url,
        data: '',
        headers: header,
      })
      .then(success)
      .catch(error)
  },

  rename(uid, params, success, error) {
    const url = `/s/data/${uid}/rename`;

    axiosInstance({
        method: 'post',
        url: url,
        data: qs.stringify(params),
        headers: header
      })
      .then(success)
      .catch(error)
  },

  info(uid) {
    return axiosInstance.get(`/s/data/${uid}/info`);
  }

};

export const groupData = {

  list(params) {
    const url = `${ baseUrl.cloud }group_data/list`;
    return axiosInstance.get(url, {
      params
    });
  },

  share(params) {
    const url = `${ baseUrl.cloud }group_data`;
    return axiosInstance.post(url, qs.stringify(params));
    // return axiosInstance.post(url, params);
  },

  async shared(uid) {
    const url = `${ baseUrl.cloud }group_data/${uid}`;
    let result;

    try {
      result = await axiosInstance.get(url)
    } catch (e) {
      result = null;
    }

    return result;
  },

  remove(params) {
    const url = `${ baseUrl.cloud }group_data`;
    return axiosInstance.delete(url, {
      data: params
    });
  }
}

export const bookmark = {

  list() {
    return axiosInstance.get(`${ baseUrl.cloud }bookmark/list`);
  },

  create(params) {
    let url = `${ baseUrl.cloud }bookmark`;
    return axiosInstance.post(url, qs.stringify(params));
  },

  update(uid, params) {
    let url = `${ baseUrl.cloud }bookmark/${uid}`;
    return axiosInstance.put(url, params);
  },

  remove(uid) {
    let url = `${ baseUrl.cloud }bookmark/${uid}`;
    return axiosInstance.delete(url);
  },

  addData(uid, params) {
    let url = `${ baseUrl.cloud }bookmark/${uid}/item`;
    return axiosInstance.post(url, qs.stringify(params));
  },

  addDatas(params) {
    let url = `${ baseUrl.cloud }bookmark/item`;
    return axiosInstance.post(url, params);
  },

  removeData(uid, itemUid) {
    let url = `${ baseUrl.cloud }bookmark/${uid}/item/${itemUid}`;
    return axiosInstance.delete(url);
  },

  dataList(uid, params) {
    // console.log(params);
    return axiosInstance.get(`${ baseUrl.cloud }bookmark/${uid}/item/list`, {
      params
    });
  },

  async getStored(uid) {
    const url = `${ baseUrl.cloud }bookmark/item/${uid}`;
    let result;

    try {
      result = await axiosInstance.get(url)
    } catch (e) {
      result = null;
    }

    return result;
  },
};

export const project = {

  list(params) {
    const url = `${ baseUrl.cloud }project/list`;
    return axiosInstance.get(url, {
      params
    });
  },

  count() {
    const url = `${ baseUrl.cloud }project/count`;
    return axiosInstance.get(url);
  },

  remove(url) {
    return axiosInstance.delete(url);
  },


}

export const app = {

  list() {
    const url = `${ baseUrl.cloud }app/list`;
    return axiosInstance.get(url);
  },

  all() {
    const url = `${ baseUrl.cloud }app/all`;
    return axiosInstance.get(url);
  },

  add(params) {
    const url = `${ baseUrl.cloud }app`;
    return axiosInstance.post(url, params);
  }
}

export const group = {

  list() {
    return axiosInstance.get(`${ baseUrl.cloud }group/list`);
  },

  memberList(uid, params) {
    const url = `${ baseUrl.cloud }group/${uid}/member/list`;
    return axiosInstance.get(url, {
      params
    })
  },

  removeMember(uid, memberUid) {
    const url = `${ baseUrl.cloud }group/${uid}/member/${memberUid}`;
    return axiosInstance.delete(url);
  },

  invite(uid, params) {
    const url = `${ baseUrl.cloud }group/${uid}/invitation`;
    return axiosInstance.post(url, qs.stringify(params));
  },

  reply(uid, params) {
    const url = `${ baseUrl.cloud }group/invitation/${uid}`;
    return axiosInstance.put(url, params);
  },

  create(params) {
    return axiosInstance.post(`${ baseUrl.cloud }group`, qs.stringify(params));
  },

  rename(uid, params) {
    const url = `${ baseUrl.cloud }group/${uid}`;
    return axiosInstance.put(url, params);
  },

  remove(uid) {
    return axiosInstance.delete(`${ baseUrl.cloud }group/${uid}`);
  },

  addMembers(uid, memberUids) {
    const url = `${ baseUrl.cloud }group/${uid}/members`;
    return axiosInstance.post(url, {
      memberUids
    });
  },
}

export const organization = {

  summary(uid) {
    const url = `${ baseUrl.cloud }organization/${uid}`;
    return axiosInstance.get(url);
  },

  memberList(uid, params) {
    const url = `${ baseUrl.cloud }organization/${uid}/member/list`;
    return axiosInstance.get(url, {
      params
    });
  },

  removeMember(uid, memberUid) {
    const url = `${ baseUrl.cloud }organization/${uid}/member/${memberUid}`
    return axiosInstance.delete(url);
  },
  updateMember(uid, memberUid, alias) {
    const url = `${ baseUrl.cloud }organization/${uid}/member/${memberUid}`
    return axiosInstance.put(url, {
      alias
    });
  }
}

export const cloudData = {

  summary() {
    const url = `${ baseUrl.cloud }data/`;
    return axiosInstance.get(url);
  },
  summary_2() {
    const url = `${ baseUrl.cloud }data_2/`;
    return axiosInstance.get(url);
  }

}

export const message = {

  list(params) {
    const url = `${ baseUrl.cloud }message/list`;
    return axiosInstance.get(url, {
      params
    });
  },

  update(uid) {
    const url = `${ baseUrl.cloud }message/${uid}`;
    return axiosInstance.put(url);
  },

  updateAll() {
    return axiosInstance.put(`${ baseUrl.cloud }message`);
  },

  async summary() {
    let url = `${ baseUrl.cloud }message/summary`;
    let result;

    try {
      result = await axiosInstance.get(url)
    } catch (e) {
      result = null;
    }

    return result;
  }

}

export const userGuide = {
  newUser() {
    const url = `${ baseUrl.cloud }user_guide/user`;
    return axiosInstance.get(url);
  },

  addNewUser() {
    const url = `${ baseUrl.cloud }user_guide/user`;
    return axiosInstance.post(url);
  }
}

export const voucher = {
  getUserVouchers() {
    const url = `${ baseUrl.cloud }vouchers`;
    return axiosInstance.get(url);
  }
}

export const job = {
  info() {
    const url = `/s/job/info`;
    return axiosInstance.get(url, );
  },
  version() {
    const url = `/s/job/version`;
    return axiosInstance.get(url);
  },

  create(type, params) {
    const url = `/s/job/${type}`;
    return axiosInstance.post(url, qs.stringify(params));
  },
  job(uid) {
    const url = `/s/job/${uid}`;
    return axiosInstance.get(url);
  },

  list(type, params, success, error) {
    const url = `/s/job/${type}/list`;
    axiosInstance.get(url, params)
      .then(success)
      .catch(error)
  },
}


export const upload = {

  upload(formData, config) {

    const url = `/s/file`;

    const promise = axiosInstance.post(url, formData, qs.stringify(config));

    return promise;
  },

  initUpload(hash, name, suffix) {

    const url = `/s/file/?hash=${hash}&multipart=true&fileName=${name}`;

    const promise = axiosInstance.post(url);

    return promise;
  },

  sliceUpload(formData, params, config) {

    let {
      uid,
      order,
      count
    } = params;

    const url = `/s/file/${uid}/part?order=${order}&count=${count}`;

    return axiosInstance.post(url, formData, config);
  },


  merge(uid) {

    const url = `/s/file/${uid}/merge`;

    const promise = axiosInstance.post(url);

    return promise;
  }
}

export const tianjing = {

  //查询群组
  userGroup() {
    const url = `${newConfig.api}/user/group`;
    return tokenInstance.get(url);
  },

  //查询部门
  userDept() {
    const url = `${newConfig.api}/user/dept`;
    return tokenInstance.get(url);
  },

  //查询注册资源 
  resourceReg(ownerType) {
    const url = `${newConfig.api}/resourceReg/query/ownertype/${ownerType}`;
    return tokenInstance.get(url);
  },

  //查询申请资源
  resourceApply(ownerType) {
    const url = `${newConfig.api}/resourceApply/query/ownertype/${ownerType}`;
    return tokenInstance.get(url);
  },

}
